package com.quectel.mobile.controller.help;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.help.FeedBackConstants;
import com.quectel.core.module.help.dto.FeedbackDto;
import com.quectel.core.module.help.service.FeedbackService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.mobile.vo.help.FeedBackReqVo;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-04-27 10:27:00
 */
@RestController
@RequestMapping("feedBack")
@Tag(name = "FeedBackController", description = "帮助中心相关api")
public class FeedBackController extends BaseController {

    @DubboReference
    private FeedbackService feedbackService;

    @PostMapping("submit")
    @Operation(summary = "提交意见反馈")
    public Response<Object> submit(@RequestBody FeedBackReqVo feedBack) {
        SessionHolder.checkHousehold();

        FeedbackDto addly = new FeedbackDto();
        addly.setTenantId(SessionHolder.getLoginMobileUserSession().getTenantId());
        addly.setVillageId(SessionHolder.getLoginMobileUserSession().getCurrentVillageId());
        addly.setPersonType(FeedBackConstants.PersonType.HOUSEHOLD.getType());
        addly.setPersonId(SessionHolder.getLoginMobileUserSession().getCurrentHousehold().getId());
        addly.setContent(feedBack.getContent());
        addly.setMobile(SessionHolder.getLoginMobileUserSession().getMobileUser().getMobile());
        addly.setImgs(feedBack.getImgs());
        feedbackService.save(addly);
        return Response.ok();
    }

    /**
     * 查看提交记录
     *
     * @return
     */
    @GetMapping("list")
    @Operation(summary = "查看提交记录列表")
    public Response<Page<FeedbackDto>> list() {
        SessionHolder.checkHousehold();

        Map<String, Object> params = new HashMap<>();
        Request request = Request.configParams(params);

        request.put(LambdaUtil.getFieldName(FeedbackDto::getPersonType), FeedBackConstants.PersonType.HOUSEHOLD.getType());
        request.put(LambdaUtil.getFieldName(FeedbackDto::getPersonId), SessionHolder.getLoginMobileUserSession().getCurrentHousehold().getId());

        int total = feedbackService.queryTotal(request.getParams());

        List<FeedbackDto> list = null;
        if (total > 0) {
            list = feedbackService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<FeedbackDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<FeedbackDto>>ok().wrap(page);
    }

}
